package com.zp.self.module.level_5_设计模式.结构型模式.Ⅰ_代理模式.动态代理_JDK_Proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author ：by Zeng Peng
 * @date ：Created 2022/6/27 21:34
 * @description ：CGLB动态代理工厂
 */
public class ProxyFactory {

    public  static <T> T getProxyInstance(Class<T> parent,Class target){
        try {
            Object targetInstance = target.getDeclaredConstructor().newInstance();
            T o1 =(T) Proxy.newProxyInstance(target.getClassLoader(), target.getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("Proxy 收取费用10块");
                    return  method.invoke(targetInstance,args);
                }
            });
            return o1;
        } catch (Exception e) {
            throw new RuntimeException("构建异常",e);
        }
    }
}
